export function timeFormat(param) {
  return param < 10 ? '0' + param : param;
}

/**
 * 倒计时
 */
export function countDown(time, callback) {
  if (typeof callback !== 'function')
    return
  var interval = setInterval(() => {
    let newTime = new Date().getTime();
    let endTime = new Date(time).getTime();

    let obj = null;
    let isEnd = false;
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000;
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      obj = {
        day: timeFormat(day),
        hou: timeFormat(hou),
        min: timeFormat(min),
        sec: timeFormat(sec)
      };
    } else { // 已结束，全部设置为'00'
      obj = {
        day: '00',
        hou: '00',
        min: '00',
        sec: '00'
      };
      isEnd = true;
      clearInterval(interval);
    }
    callback(isEnd, obj);
  }, 1000);
  return interval
}